SUIPatternLibrary.ContentScriptController = (function($, _, app){
var
	
	active = false,

    init = function(){
        initConnection();
        app.Model.on("ready", function(){
            var engine = app.Model.getEngineByLocation(location.host);
            SUIPatternLibrary.EngineView.init(engine);
        });
        initEvents();
    },

    initConnection = function(){
        chrome.extension.onConnect.addListener(function(port) {
            port.onMessage.addListener(function(event){
                onExtensionMessage(event.type, event.data);
            });
        });
    },

    initEvents = function(){
        app.EngineView.on("patternClick", function(event, patternNamesJSON){
            var patternNames = JSON.parse(patternNamesJSON);
            if(patternNames.length < 2){
                displayPatternDetails(patternNames[0]);
            }
            else{
                displayPatternSelectDialog(patternNames);
            }
        });
        app.EngineView.on("patternHover", function(event, patternName){
            highlightPattern(patternName);
        });
    },

    onExtensionMessage = function(event, data){
        switch (event){
            case "patternClick" : {
                var patternName = data;
                displayPatternDetails(patternName);
                break;
            }
            case "patternHover" : {
                var patternName = data;
                highlightPattern(patternName);
                break;
            }
            case "engineSelected" : {
                var engineName = data;
                changeLocation(engineName);
                break;
            }
			case "activate" : {
				active = data;
				break;
			}
            default : {
                console.log("unsupported event", event);
            }
        }
    },

    changeLocation = function(engineName){
        if(engineName == undefined) return;
        if(location.host.split(".")[1].toLowerCase() != engineName.toLowerCase()){
            console.log(engineName);
            window.location = app.Model.getEngineByName(engineName).url;
        }
    },

    displayPatternSelectDialog = function(patternNames){
        var dialog = app.PatternSelectDialog();
        dialog.init(patternNames);
        $(dialog).on("patternClick", function(event, patternName){
            displayPatternDetails(patternName);
        });
    },

    displayPatternDetails = function(patternName){
        var pattern = app.Model.getPatternByName(patternName);
        if(pattern == undefined) return;
        var detailView = app.PatternDetailView();
        detailView.init(pattern);
        detailView.on("patternClick", function(event, patternName){
            displayPatternDetails(patternName);
        });
    },

    highlightPattern = function(patternName){
		if(active){
			if(patternName == undefined){
				app.EngineView.unhighlightAll();
			}
			app.EngineView.highlightPattern(patternName);
		}
    };

    return {
        init: init
    };

}(jQuery, _, SUIPatternLibrary));
